
/*
 * Medication.fx
 *

 A Medication is a collection of Orders
 
 * Created on Oct 30, 2009, 7:45:14 AM
 */

package GraphsWindowPackage.MedicationPackage;
import javafx.util.Math;
import GraphsWindowPackage.Entry;

/**
 * @author quang
 */


public class Medication extends Entry {
    public var orders: Order[]; //sequence of orders

    //returns true if this medication is active i.e. has an order within
    //24 hours w.r.t currentTime
    public function isActive(currentTime: Number): Boolean {
        for (i in [sizeof orders - 1..0]){
            if (orders[i].finishTime <= currentTime and orders[i].finishTime >= currentTime - 1) return true
        }
        return false
    }


    public override function createEntryInTimeRange(minTime: Number, maxTime: Number): Medication {

        //if minRange > maxRange then return an empty medication
        if (minTime > maxTime) {
            return null;
        }
        else {
            var tempOrders: Order[];
            var tempMinTime: Number = minTime;
            var tempMaxTime: Number = maxTime;

            for (i in [0..sizeof this.orders - 1]) {
                //if this order is one time order
                if (this.orders[i].isOnce) {
                    if (maxTime >= this.orders[i].startTime and this.orders[i].startTime >= minTime)
                        insert orders[i] into tempOrders
                }
                else //this order is period order
                {
                    tempMinTime = Math.max(this.orders[i].startTime, minTime);
                    tempMaxTime = Math.min(this.orders[i].finishTime, maxTime);
                    if (tempMinTime <= tempMaxTime)
                        insert Order {
                            startTime: tempMinTime
                            finishTime: tempMaxTime
                            isOnce: false
                            info: this.orders[i].info
                        } into tempOrders;
                }
            }
            
            return Medication {
                orders: tempOrders;
            }                
        }
    }
}
